package com.doordu.soa.service.web.fieldvalidator;

import com.doordu.soa.service.web.annotation.DataValid;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zjp on 2017/4/19.
 */
public class ValidatorFactory {
    private static final Map<Class, BaseValidatorHandler> validatorHandler = new HashMap<>(5);
    private static String NO_ERROR = "";

    static {
            validatorHandler.put(Integer.class, new IntegerValidatorHandler());
            validatorHandler.put(IntegerChoiceHandler.class, new IntegerChoiceHandler());
            validatorHandler.put(DateValidatorHandler.class, new DateValidatorHandler());
    }

    public static <T> String checkField(DataValid dataValid, Object bean, T value) {
        Class cls = dataValid.checkHandler();
        if (validatorHandler.containsKey(cls)) {
            return validatorHandler.get(cls).validate(dataValid, bean, value);
        }
        return NO_ERROR;
    }
}
